﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SanctusSoftware.CcliSongSelectLib.Management;
using System.ComponentModel;
using System.IO;


namespace SanctusSoftware.CcliSongSelectImporter
{
    /// <summary>
    /// Interaction logic for LibraryWindow.xaml
    /// </summary>
    public partial class LibraryWindow : Window, INotifyPropertyChanged
    {

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region Properties

        private Library _Library = null;
        public Library Library
        {
            get { return _Library; }
            set
            {
                _Library = value;
                OnPropertyChanged("Library");
            }
        }

        public ErrorInfo<Library> LibraryValidation { get; set; }

        #endregion

        #region Constructor

        public LibraryWindow(Library library)
        {
            InitializeComponent();
            this.DataContext = this;
            this.Library = library; 
        }

        #endregion

        private void _DirectoryButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this.Library.Path = dialog.SelectedPath;
                }   
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }   
        }

        private void _OKButton_Click(object sender, RoutedEventArgs e)
        {
            string path = _DirectoryTextBox.Text ;
            if (string.IsNullOrWhiteSpace(path))
            {
                MessageBox.Show("Directory cannot be blank.");
            }
            else if (!Directory.Exists(path))
            {
                MessageBox.Show("Directory is invalid");
            }
            else
            {
                if (LibraryValidation != null)
                {
                    string[] errors = LibraryValidation.Invoke(Library);
                    if (errors != null && errors.Length > 0)
                    {
                        MessageBox.Show(string.Join(System.Environment.NewLine, errors));
                        return;
                    }
                }

                this.DialogResult = true;
                this.Close();
            }
        }

        private void _CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }




        
    }
}
